﻿using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace SingleBlog.Core.Data
{
    public class SingleBlogDbContext : IdentityDbContext<SingleBlogUser,SingleBlogRole,int>
    {
        public SingleBlogDbContext()
        {

        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source=SingleBlog.db");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Category>()
                .HasMany(m => m.Articles).WithOne(m => m.Category).HasForeignKey(m => m.CategoryId);

            modelBuilder.Entity<Comment>()
                .HasMany(m => m.ChildComments).WithOne(m => m.ParentComment).HasForeignKey(m => m.ParentId);

            modelBuilder.Entity<Article>()
                .HasMany(m => m.Comments).WithOne(m => m.Article).HasForeignKey(m => m.ArticleId);

            modelBuilder.Entity<SingleBlogUser>().HasKey(m => m.Id);                
            modelBuilder.Entity<SingleBlogUser>()
                .ToTable("Users")
                .HasMany(m => m.Articles).WithOne(m => m.User)
                .HasForeignKey(m => m.UserId);
            modelBuilder.Entity<SingleBlogUser>().Property(m => m.Id).ValueGeneratedOnAdd();

            modelBuilder.Entity<SingleBlogRole>()
                .HasKey(m => m.Id);
            modelBuilder.Entity<SingleBlogRole>()
                .ToTable("Roles")
                .Property(m => m.Id).ValueGeneratedOnAdd()                
                ;
            modelBuilder.Entity<IdentityUserRole<int>>().ToTable("UserInRoles");
            modelBuilder.Entity<IdentityUserRole<int>>().HasKey(m => new { m.UserId, m.RoleId });

            modelBuilder.Entity<IdentityRoleClaim<int>>().ToTable("RoleClaims");
            modelBuilder.Entity<IdentityRoleClaim<int>>().HasKey(m => m.Id);
            modelBuilder.Entity<IdentityUserClaim<int>>().ToTable("UserClaims");
            modelBuilder.Entity<IdentityUserClaim<int>>().HasKey(m => m.Id);
            modelBuilder.Entity<IdentityUserLogin<int>>().ToTable("UserLogins");
            modelBuilder.Entity<IdentityUserLogin<int>>().HasKey(m => new { m.UserId, m.ProviderKey});
            modelBuilder.Entity<IdentityUserToken<int>>().ToTable("UserTokens");
            modelBuilder.Entity<IdentityUserToken<int>>().HasKey(m => new { m.UserId, m.LoginProvider });
        }

        /// <summary>
        /// 文章
        /// </summary>
        public DbSet<Article> Articles { get; set; }

        /// <summary>
        /// 设置
        /// </summary>
        public DbSet<Setting> Settings { get; set; }

        /// <summary>
        /// 分类
        /// </summary>
        public DbSet<Category> Categories { get; set; }
        /// <summary>
        /// 回复
        /// </summary>
        public DbSet<Comment> Comments { get; set; }
        /// <summary>
        /// 导航。
        /// </summary>
        public DbSet<Navigation> Navigations { get; set; }
    }
}
